package ru.spb.osll.myfit.server.services;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import ru.spb.osll.myfit.client.service.ChartComparisonService;
import ru.spb.osll.myfit.common.ComparisonResult;
import ru.spb.osll.myfit.common.Range;
import ru.spb.osll.myfit.common.chart.ChartData;
import ru.spb.osll.myfit.server.chart.ChartHandler;
import ru.spb.osll.myfit.server.comparing.ChartComparator;
import ru.spb.osll.myfit.server.comparing.Ratio;
import ru.spb.osll.myfit.server.comparing.Mean;

import java.util.*;

public class ChartComparisonServiceImpl extends RemoteServiceServlet implements ChartComparisonService {
  private ChartHandler m_chartHandler = new ChartHandler();
  private Map<String, ChartComparator> m_comparators = new HashMap<String, ChartComparator>();

  public ChartComparisonServiceImpl() {
    ChartComparator compareByMean = new Mean(m_chartHandler);
    m_comparators.put(compareByMean.id(), compareByMean);

    ChartComparator divide = new Ratio(m_chartHandler);
    m_comparators.put(divide.id(), divide);
  }

  @Override
  public List<ChartData> getChartDataList(List<String> ids) {
    return m_chartHandler.getChartDataList(ids);
  }

  @Override
  public Set<String> getComparators() {
    Set<String> comparators = new TreeSet<String>(m_comparators.keySet());
    return comparators;
  }

  @Override
  public ComparisonResult compare(String id, List<String> ids, Range range) {
    return m_comparators.get(id).compare(ids, range);
  }
}
